package com.zhang.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.pojo.CustomBean;
import com.zhang.pojo.SupplierBean;
import com.zhang.service.ICustomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "客户控制层接口")
@RequestMapping("custom")
@RestController
public class CustomController {

    @Autowired
    private ICustomService customService;

    @ApiOperation("查询所有的客户")
    @GetMapping("findAll")
    public List<CustomBean> findAll(){
        return customService.findAll();
    }

    @ApiOperation("根据客户名和范围动态查询并分页")
    @GetMapping("findPageAndItem/{current}/{pageSize}/{name}/{scope}")
    public IPage<CustomBean> findPageAndItem(@PathVariable("current")int current,
                                             @PathVariable("pageSize")int pageSize,
                                             @PathVariable("name")String name,
                                             @PathVariable("scope")String scope){
        if(!name.equals("") &&"1".equals(name) ){
            name = null;
        }
        if(!scope.equals("") &&"1".equals(scope)){
            scope = null;
        }
        return customService.findByPage(new Page<>(current,pageSize),name,scope);
    }


    @ApiOperation("添加客户")
    @PostMapping("add")
    public String add(CustomBean customBean){
        try {
            customService.add(customBean);
            return "ok";
        } catch (Exception e) {
            return "no";
        }
    }

    @ApiOperation("修改客户信息")
    @PutMapping("update")
    public String update(CustomBean customBean){
        try {
            customService.update(customBean);
            return "ok";
        } catch (Exception e) {
            return "no";
        }
    }

    @ApiOperation("根据客户id查询客户信息")
    @GetMapping("findById")
    public CustomBean findById(int id){
        return customService.findById(id);
    }
}
